from django.contrib import admin
from django.core.cache import cache
from treebeard.admin import TreeAdmin
from treebeard.forms import movenodeform_factory
from .models import Role, Post, Dept, Menu, MenuField, FieldPermission, MenuButton, RoleMenuPermission, \
    RoleMenuButtonPermission, Dictionary, Area, ApiWhiteList, SystemConfig

default_list_per_page = 50


@admin.register(Role)
class RoleAdmin(admin.ModelAdmin):
    list_display = ('name', 'key', 'description', 'status')
    list_display_links = ('name',)
    list_filter = ('status',)
    search_fields = ('name', 'key')
    ordering = ('sort',)
    list_per_page = default_list_per_page
    date_hierarchy = 'create_time'


@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'status')
    list_display_links = ('name',)
    list_filter = ('status',)
    search_fields = ('name', 'code')
    ordering = ('sort',)
    list_per_page = default_list_per_page
    date_hierarchy = 'create_time'


@admin.register(Dept)
class DeptAdmin(TreeAdmin):
    form = movenodeform_factory(Dept)
    list_display = ('id', 'name', 'key', 'owner', 'phone', 'email', 'status')
    list_display_links = ('name',)
    list_filter = ('status',)
    search_fields = ('name', 'key')
    # ordering = ('sort',)
    list_per_page = 1000
    date_hierarchy = 'create_time'


@admin.register(Menu)
class MenuAdmin(TreeAdmin):
    form = movenodeform_factory(Menu)
    list_display = ('name', 'is_catalog', 'sort', 'icon', 'status')
    list_display_links = ('name',)
    list_filter = ('status',)
    search_fields = ('name',)
    # ordering = ('sort',)
    list_per_page = 1000
    date_hierarchy = 'create_time'


@admin.register(MenuField)
class MenuFieldAdmin(admin.ModelAdmin):
    list_display = ('id', 'menu', 'title', 'model')
    list_display_links = ('id', 'menu', 'title',)
    list_filter = ('model',)
    search_fields = ('title', 'model')
    ordering = ('-id',)
    list_per_page = default_list_per_page
    date_hierarchy = 'create_time'


@admin.register(FieldPermission)
class FieldPermissionAdmin(admin.ModelAdmin):
    list_display = ('id', 'role', 'field', 'is_query', 'is_create', 'is_update')
    list_display_links = ('id', 'role')
    list_filter = ('role', 'field')
    # search_fields = ('name', 'key')
    ordering = ('-id',)
    list_per_page = default_list_per_page
    date_hierarchy = 'create_time'


@admin.register(MenuButton)
class MenuButtonAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'value', 'api', 'menu', 'method')
    list_display_links = ('id', 'name',)
    list_filter = ('method',)
    search_fields = ('name',)
    ordering = ('-id',)
    list_per_page = default_list_per_page
    date_hierarchy = 'create_time'


@admin.register(RoleMenuPermission)
class RoleMenuPermissionAdmin(admin.ModelAdmin):
    list_display = ('id', 'role', 'menu')
    list_display_links = ('id', 'role')
    list_filter = ('role', 'menu')
    # search_fields = ('name', 'key')
    ordering = ('-id',)
    list_per_page = default_list_per_page
    date_hierarchy = 'create_time'


@admin.register(RoleMenuButtonPermission)
class RoleMenuButtonPermissionAdmin(admin.ModelAdmin):
    list_display = ('id', 'role', 'menu_button', 'data_range')
    list_display_links = ('id', 'role')
    list_filter = ('role', 'menu_button')
    # search_fields = ('name', 'key')
    ordering = ('-id',)
    list_per_page = default_list_per_page
    date_hierarchy = 'create_time'


@admin.register(Dictionary)
class DictionaryAdmin(TreeAdmin):
    form = movenodeform_factory(Dictionary)
    list_display = ('label', 'value', 'type', 'status')
    list_display_links = ('label',)
    list_filter = ('status',)
    search_fields = ('label', 'value')
    ordering = ('sort',)
    list_per_page = 1000
    date_hierarchy = 'create_time'


@admin.register(Area)
class AreaAdmin(TreeAdmin):
    form = movenodeform_factory(Area)
    list_display = ('name', 'code', 'level', 'pinyin', 'status')  # 'id',
    list_display_links = ('name',)
    list_filter = ('status', 'level')
    search_fields = ('name', 'code')
    # ordering = ('-id',)
    list_per_page = 5000
    date_hierarchy = 'create_time'

    # 效果不大，应该是渲染数据比较费时。
    # def changelist_view(self, request, extra_context=None):
    #     # 尝试从缓存中获取数据
    #     cached_data = cache.get('yc_area_model_data')
    #     if not cached_data:
    #         # 如果缓存不存在，从数据库中获取数据
    #         qs = super().get_queryset(request)
    #         cache.set('yc_area_model_data', qs, timeout=60 * 15)  # 缓存 15 分钟
    #     else:
    #         # 如果缓存存在，使用缓存数据
    #         qs = cached_data
    #
    #     extra_context = extra_context or {}
    #     extra_context['yc_area_model_data'] = qs
    #     return super().changelist_view(request, extra_context=extra_context)


@admin.register(ApiWhiteList)
class ApiWhiteListAdmin(admin.ModelAdmin):
    list_display = ('id', 'url', 'method', 'enable_datasource')
    list_display_links = ('url',)
    list_filter = ('method',)
    search_fields = ('url',)
    ordering = ('-id',)
    list_per_page = default_list_per_page
    date_hierarchy = 'create_time'


@admin.register(SystemConfig)
class SystemConfigAdmin(TreeAdmin):
    form = movenodeform_factory(SystemConfig)
    list_display = ('title', 'key', 'value')
    list_display_links = ('title',)
    list_filter = ('status',)
    search_fields = ('title',)
    ordering = ('sort',)
    list_per_page = 1000
    date_hierarchy = 'create_time'
